package com.fenghongzhang.day018;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.squareup.picasso.Picasso;

import java.util.List;

public class MyAdapter extends BaseAdapter {

        private List<Food.DataBean> list;
        private Context context;

        public MyAdapter(List<Food.DataBean> list, Context context) {
            this.list = list;
            this.context = context;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getViewTypeCount() {
            return 2;
        }

        @Override
        public int getItemViewType(int position) {
            if (position % 2 == 0){
                return 0;
            }
            return 1;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Vhleft vhleft;
            Vhright vhright;
            if (convertView == null){
                if (getItemViewType(position) == 0){
                    vhleft = new Vhleft();
                    convertView = LayoutInflater.from(context).inflate(R.layout.left_layout,null);
                    vhleft.imageView = convertView.findViewById(R.id.left_img);
                    vhleft.textView = convertView.findViewById(R.id.left_title);
                    convertView.setTag(vhleft);
                }else {
                    vhright = new Vhright();
                    convertView = LayoutInflater.from(context).inflate(R.layout.right,null);
                    vhright.imageView = convertView.findViewById(R.id.right_img);
                    vhright.textView = convertView.findViewById(R.id.right_title);
                    convertView.setTag(vhright);
                }
            }
            if (getItemViewType(position) == 0){
                vhleft = (Vhleft) convertView.getTag();
//                vhleft.imageView.setImageResource("");
                //load 下载的网址,可以是网址,也可以是本地文件,也可以是资源文件.
//                Picasso.with(context).load(list.get(position).getPic()).into(vhleft.imageView);
                Glide.with(context).load(list.get(position).getPic()).apply(new RequestOptions().circleCrop()).into(vhleft.imageView);
                vhleft.textView.setText(list.get(position).getTitle());
            }else {
                vhright = (Vhright) convertView.getTag();
//                Picasso.with(context).load(list.get(position).getPic()).into(vhright.imageView);
                Glide.with(context).load(list.get(position).getPic()).apply(new RequestOptions().circleCrop()).into(vhright.imageView);
                vhright.textView.setText(list.get(position).getTitle());
            }

            return convertView;
        }

        class Vhleft {
            ImageView imageView;
            TextView textView;
        }
        class Vhright {
            ImageView imageView;
            TextView textView;
        }

    }
